package com.pakazaka.player;

import com.pakazaka.enums.PlayerStateType;
import com.pakazaka.roguelike.ui.MainInfo;
import com.pakazaka.simplecrawler.Core;
import static com.pakazaka.simplecrawler.Core.getInstance;
import com.pakazaka.testItemsSystem.Item;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Remi
 */
public class Player extends BasicStranger {
    private PlayerStateType state = PlayerStateType.NORMAL;
    //private EffectType hungryEffect, thirstEffect, drunkEffect, intoxicatedEffect, smokyEffect;
    private int hungryPowah, thirstPowah, drunkPowah, intoxicatedPowah, smokyPowah, tuneUpPowah;
    //stats
    private int strength = 10, intelligence = 8, dexterity = 12, stamina = 9;
    private static final int BUFF_MAX_VALUE = 50;
    private static final String[] thirstStates = {"normal", "weak thirst", "thirst", "excessive thirst", "dehydration", "at death's door"};
    private static final String[] drunkStates = {"normal", "weak drunk", "drunk", "excessive drunk", "unconsciousness", "at death's door"};
    private static final String[] smokyStates = {"normal", "weak", "smoky", "excessive", "wat", "at death's door"};
    private static final String[] hungryStates = {"normal", "weak", "hungry", "excessive", "wat", "at death's door"};
    private static final String[] intoxicatedStates = {"normal", "weak", "intoxicated", "excessive", "wat", "at death's door"};

    public enum EffectType {
        NONE,
        DRUNKENNESS,
        INTOXICATED,
        SMOKY,
        HUNGER,
        THIRST,
        TUNE_UP,
        HEALING,
        DAMAGE,
    }

    public PlayerStateType getState() {
        return state;
    }

    public void setState(PlayerStateType state) {
        this.state = state;
    }

    public Player(String pathToSprite) {
        super(pathToSprite);
    }

    public int getBaseStrength() {
        return strength;
    }

    public int getBaseIntelligence() {
        return intelligence;
    }

    public int getBaseDexterity() {
        return dexterity;
    }

    public int getBaseStamina() {
        return stamina;
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        MainInfo.paint(g, this);
        g2d.drawImage(sprite, getPixelX(), getPixelY(), null);
    }

    public void dropItem(Item item, int x, int y) {
        item.setBlocksX(x);
        item.setBlocksY(y);
        getInstance().getItemsOnMap().add(item);
        getInventory().remove(item);
        volume -= item.getVolume();
    }

    //TODO delete
    public void processPockets() {
        for (int i = 0; i < getEquip().getEquipArray().length; i++) {
            if (getEquip().getEquippedItem(i) != null) {
            }
        }
    }

    /**
     *
     * @param effect
     * @param powah if positive, then effect will grow, else fall
     */
    public void applyEffect(Player.EffectType effect, int powah) {
        switch (effect) {
            case DRUNKENNESS:
                drunkPowah = fitInLimits(drunkPowah, powah, 0, BUFF_MAX_VALUE);
                break;
            case HUNGER:
                hungryPowah = fitInLimits(hungryPowah, powah, 0, BUFF_MAX_VALUE);
                break;
            case INTOXICATED:
                intoxicatedPowah = fitInLimits(intoxicatedPowah, powah, 0, BUFF_MAX_VALUE);
                break;
            case SMOKY:
                smokyPowah = fitInLimits(smokyPowah, powah, 0, BUFF_MAX_VALUE);
                break;
            case THIRST:
                thirstPowah = fitInLimits(thirstPowah, powah, 0, BUFF_MAX_VALUE);
                break;
            case TUNE_UP:
                tuneUpPowah = fitInLimits(tuneUpPowah, powah, 0, BUFF_MAX_VALUE);
                break;
        }
        Core.getInstance().getConsole().pushDebugMessage("\r\nEffect \"" + effect.name() + "\" is applied " + " (power: " + powah + ")");
    }

    public String getDebuffState(EffectType debuff) {
        switch (debuff) {
            case THIRST:
                return thirstStates[thirstPowah / 10];
            case DRUNKENNESS:
                return drunkStates[drunkPowah / 10];
            case SMOKY:
                return smokyStates[smokyPowah / 10];
            case HUNGER:
                return hungryStates[hungryPowah / 10];
            case INTOXICATED:
                return intoxicatedStates[intoxicatedPowah / 10];
            default:
                return "unknown";
        }
    }

    public int getStrengthPenalty() {
        return thirstPowah / 5;
    }

    public int getStaminaPenalty() {
        return 0;
    }

    public int getIntelligencePenalty() {
        return 0;
    }

    public int getDexterityPenalty() {
        return 0;
    }

    public void updateEffects() {
        if (thirstPowah >= 50 || drunkPowah >= 50 || smokyPowah >= 50 || intoxicatedPowah >= 50 || hungryPowah >= 50) {
            setState(PlayerStateType.CANNOT_MOVE);
        }
    }

    private int fitInLimits(int value, int inc, int downLimit, int upLimit) {
        int result = value + inc;
        if (value + inc > upLimit) {
            result = upLimit;
        } else if (value + inc < downLimit) {
            result = downLimit;
        }
        return result;
    }
}
